//
//  UIViewController+EmptyDataSet.m
//  suRead
//
//  Created by lidi on 2018/2/2.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "UIViewController+EmptyDataSet.h"
#import "Masonry.h"
#import <objc/runtime.h>

@interface UIViewController()
@property (nonatomic, copy) EmptyDataSetCallBack reloadCallback;
@property (nonatomic, strong) UIView *emptyDataNoticeView;
@end

@implementation UIViewController (EmptyDataSet)

static char reloadCallbackKey;
static char emptyDataNoticeViewKey;

- (EmptyDataSetCallBack)reloadCallback {
    return objc_getAssociatedObject(self, &reloadCallbackKey);
}

- (void)setReloadCallback:(EmptyDataSetCallBack)reloadCallback_ {
    objc_setAssociatedObject(self, &reloadCallbackKey, reloadCallback_, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIView *)emptyDataNoticeView {
    return objc_getAssociatedObject(self, &emptyDataNoticeViewKey);
}

- (void)setEmptyDataNoticeView:(UIView *)emptyDataNoticeView_ {
    objc_setAssociatedObject(self, &emptyDataNoticeViewKey, emptyDataNoticeView_, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//MARK: - 清除提示
- (void)emptyDataSetClearAllNotice {
    if (self.emptyDataNoticeView) {
        [self.emptyDataNoticeView removeFromSuperview];
        self.emptyDataNoticeView = nil;
    }
}

//MARK: - 添加断网提示
- (void)emptyDataSetShowNetWorkAvaliable:(EmptyDataSetCallBack)reloadHandle {
    [self setReloadCallback:reloadHandle];
    
    if (self.emptyDataNoticeView) {
        for (UIView *subView in [self.emptyDataNoticeView subviews]){
            [subView removeFromSuperview];
        }
        [self.emptyDataNoticeView removeFromSuperview];
    }
    
    UIView *netWorkAvaliableView = [UIView new];
    
    UIView *noDataContentView = [UIView new];
    [netWorkAvaliableView addSubview:noDataContentView];
    [noDataContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(noDataContentView.superview);
        make.width.equalTo(noDataContentView.superview);
    }];
    
    UIImageView *noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notice_empty_icon"]];
    [noDataContentView addSubview:noDataImageView];
    [noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noDataImageView.superview);
        make.centerX.equalTo(noDataImageView.superview);
        make.width.equalTo(@60.0f);
        make.height.equalTo(@60.0f);
    }];
    
    UILabel *reloadNotice = [UILabel new];
    reloadNotice.text = @"网络异常，点击屏幕重试";
    reloadNotice.textColor = [UIColor lightGrayColor];
    [noDataContentView addSubview:reloadNotice];
    [reloadNotice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noDataImageView.mas_bottom).offset(5.0);
        make.centerX.equalTo(reloadNotice.superview);
    }];
    
    UIView *targetView = self.view;
    for (UIView *subview in self.view.subviews) {
        if ([subview isKindOfClass:[UITableView class]]) {
            targetView = subview;
        }
        if ([subview isKindOfClass:[UICollectionView class]]) {
            targetView = subview;
        }
    }
    
    if (targetView == self.view) {
        [self.view addSubview:netWorkAvaliableView];
    } else {
        [self.view insertSubview:netWorkAvaliableView aboveSubview:targetView];
    }
    
    [netWorkAvaliableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(netWorkAvaliableView.superview);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reload:)];
    netWorkAvaliableView.userInteractionEnabled = YES;
    [netWorkAvaliableView addGestureRecognizer:tap];
    
    self.emptyDataNoticeView = netWorkAvaliableView;
}

- (void)emptyDataSetShowEmptyData:(NSString *)notice {
    
    UIView *netWorkAvaliableView = [UIView new];
    netWorkAvaliableView.userInteractionEnabled = NO;
    
    UIView *noDataContentView = [UIView new];
    [netWorkAvaliableView addSubview:noDataContentView];
    [noDataContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(noDataContentView.superview);
        make.width.equalTo(noDataContentView.superview);
    }];
    
    UIImageView *noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notice_empty_icon"]];
    [noDataContentView addSubview:noDataImageView];
    [noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noDataImageView.superview);
        make.centerX.equalTo(noDataImageView.superview);
        make.width.equalTo(@60.0f);
        make.height.equalTo(@60.0f);
    }];
    
    UILabel *reloadNotice = [UILabel new];
    reloadNotice.text = notice;
    reloadNotice.textColor = [UIColor lightGrayColor];
    [noDataContentView addSubview:reloadNotice];
    [reloadNotice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noDataImageView.mas_bottom).offset(5.0);
        make.centerX.equalTo(reloadNotice.superview);
    }];
    
    UIView *targetView = self.view;
    for (UIView *subview in self.view.subviews) {
        if ([subview isKindOfClass:[UITableView class]]) {
            targetView = subview;
        }
        if ([subview isKindOfClass:[UICollectionView class]]) {
            targetView = subview;
        }
    }
    
    if (targetView == self.view) {
        [self.view addSubview:netWorkAvaliableView];
    } else {
        [self.view insertSubview:netWorkAvaliableView aboveSubview:targetView];
    }
    
    [netWorkAvaliableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(netWorkAvaliableView.superview);
    }];
    
    self.emptyDataNoticeView = netWorkAvaliableView;
}

- (void)reload:(id)sender {
    if (self.reloadCallback) {
        self.reloadCallback();
    }
    [self emptyDataSetClearAllNotice];
}

@end
